//
//  ExerRecordVc.m
//  CareRingApp
//
//  Created by Linktop on 2023/7/25.
//

#import "ExerRecordVc.h"
#import <Masonry/Masonry.h>
#import "UIViewController+Custom.h"
#import "ConfigModel.h"
#import "DeviceCenter.h"
#import "DBExercise.h"

#import "SRDeviceInfo+description.h"
#import "ExerciseCell.h"
#import "UITableViewCell+Styles.h"
#import "ExerDetailVc.h"
#import <DateTools/DateTools.h>
#import <MJRefresh/MJRefresh.h>

@interface ExerRecordVc ()<UITableViewDelegate, UITableViewDataSource>
@property(strong, nonatomic)UITableView *tableView;

@property(strong, nonatomic)NSMutableArray <DBExercise *> *dataArray;
@property(strong, nonatomic)NSDateFormatter *fmt;
@property(strong, nonatomic)NSNumber *selectYear;
@end

@implementation ExerRecordVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if(!self.dataArray.count) {
        [self queryData:self.selectYear.integerValue];
    }

}


-(void)initUI {
    
    [self arrowback:nil];
    self.navigationItem.title = _L(L_NAV_TITLE_EXERCISE);
//    [self setCustomTitle:_L(L_NAV_TITLE_EXERCISE)];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.tableView.superview).offset(VC_ITEM_MARGIN_HOR);
        make.trailing.equalTo(self.tableView.superview).inset(VC_ITEM_MARGIN_HOR);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(pushFresh)];
    
}

-(void)queryData:(NSInteger)year {
    WEAK_SELF
    [DBExercise queryBy:[DeviceCenter instance].bindDevice.macAddress Year:year OrderBeTimeDesc:YES Cpmplete:^(NSMutableArray<DBExercise *> * _Nonnull results) {
        STRONG_SELF
        if (results.count) {
            if (!strongSelf.dataArray) {
                strongSelf.dataArray = [NSMutableArray new];
            }
            
            [strongSelf.dataArray addObjectsFromArray:results];
            
            [strongSelf.tableView reloadData];
        }
        
    }];

    
}

-(void)pushFresh {
    [self queryData:self.selectYear.integerValue - 1];
//    [self.tableView.mj_footer endRefreshing];
    
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ExerciseCell class] forCellReuseIdentifier:NSStringFromClass([ExerciseCell class])];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0.0f;
        }
        
        WEAK_SELF
      
//        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//            [weakSelf pushFresh];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [weakSelf.tableView.mj_footer endRefreshing];
//
//            });
//
//        }];
//        MJRefreshAutoFooter *footer = (MJRefreshAutoFooter *)self.tableView.mj_footer;
//        footer. = YES;

    }
    
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    ExerciseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ExerciseCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSMutableArray<DBExercise *> *recs = self.dataArray;
    
    cell.exerObj = recs[indexPath.row];
    
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return  self.dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    ExerDetailVc *vc = [[ExerDetailVc alloc]init];
    vc.exerObj = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    

}

-(NSNumber *)selectYear
{
    if (!_selectYear) {
        _selectYear = @([[NSDate date] year]);
    }
    return _selectYear;
}

-(NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.dateFormat = _L(L_BIRTH_FORMAT);
    }
    return _fmt;
}
@end
